package com.atguigu.Huffmancode;

import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * description:
 * Created by 才睡醒 on 2021-07-30
 */
public class Huffmancode {
    public static void main(String[] args) {
         String content = "i like like like java do you like a java";
        byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
        System.out.println(contentBytes.length);
        List<Node> nodes = getNodes(contentBytes);
        System.out.println(nodes);
        //测试一把
        System.out.println("huffmantree");
        Node huffmanTree = createHuffmanTree(nodes);
        //前序遍历
        huffmanTree.preOrder();

    }
    //前序遍历
    private static void preOrder(Node root){
        if (root !=null){
            root.preOrder();
        }else {
            System.out.println("null");
        }
    }

    /**
     * 接收一个字节数组
     * 返回一个List
     * @param bytes
     * @return
     */
    private static List<Node> getNodes(byte[] bytes){
        //创建一个ArrayList
        ArrayList<Node> nodes = new ArrayList<>();
        //存储每个byte出现的次数 - map
        HashMap<Byte, Integer> counts = new HashMap<>();
        for (byte b :bytes){
            Integer count = counts.get(b);
            if(count == null){ //没有这个字符数据
                counts.put(b,1);
            }else {
                counts.put(b,count+1);
            }
        }
        //把每一个键值对转成一个Node对象，并加入到Nodes集合
        //遍历map
        for (Map.Entry<Byte, Integer> entry : counts.entrySet()) {
            nodes.add(new Node(entry.getKey(),entry.getValue()));
        }
        return nodes;
    }
    //通过list创建对应的赫夫曼树
    private static Node createHuffmanTree(List<Node> nodes){
        while (nodes.size() >1){
            //排序,从小到大
            Collections.sort(nodes);
            //取出第一颗最小的二叉树
            Node leftNode = nodes.get(0);
            Node rightNode = nodes.get(1);
            //创建一颗新的二叉树，它的根结点没有data，只有权值
            Node parent = new Node(null,leftNode.weight+ rightNode.weight);
            parent.left = leftNode;
            parent.right = rightNode;
            //将已经处理的两颗二叉树从nodes删除
            nodes.remove(leftNode);
            nodes.remove(rightNode);
            //将新的二叉树加入到nodes
            nodes.add(parent);
        }
        //nodes最后的结点就是赫夫曼树的根节点
        return nodes.get(0);
    }
}

class Node implements Comparable<Node>{
    public Byte data;  //存放数据本身，比如‘a’=97
    public int weight; //权值，表示字符出现的次数
    Node left;
    Node right;

    public Node(Byte data, int weight) {
        this.data = data;
        this.weight = weight;
    }

    @Override
    public int compareTo(Node o) {
        return this.weight - o.weight;
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                ", weight=" + weight +
                '}';
    }

    //前序遍历
    public void preOrder(){
        System.out.println(this);
        if(this.left !=null){
            this.left.preOrder();
        }
        if(this.right != null){
            this.right.preOrder();
        }
    }
}
